<?php
namespace App\Services\Trade;

use App\Models\Trade\Search\SearchRecordModel;
use App\Models\Trade\Common\FishKindModel;

class SearchService
{

    /**
     * 保存记录
     *
     * @param string $keyword            
     * @throws ServiceException
     * @return boolean
     */
    public function saveRecord($keyword)
    {
        $item = SearchRecordModel::findTodayByName($keyword)->shift();
        if (! $item) {
            $data['name'] = $keyword;
            $data['kind_id'] = 0;
            $data['amount'] = 1;
            // 品种查询
            $kind = FishKindModel::findByName($keyword)->shift();
            if ($kind && $kind['status'] === FishKindModel::STATUS_DEFAULT) {
                $data['kind_id'] = $kind['id'];
            }
            // 数量初始化
            $number = SearchRecordModel::addItem($data);
        } else {
            $data['amount'] = $item['amount'] + 1;
            // 数量 +1
            $number = SearchRecordModel::editItem($item['id'], $data);
        }
        
        return (boolean) $number;
    }
}